﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class AdminRole
    {
        public int Add(KeRui.Model.AdminRole model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4), new SqlParameter("@RoleName", SqlDbType.NVarChar, 20), new SqlParameter("@RoleFlag", SqlDbType.VarChar), new SqlParameter("@AdminNum", SqlDbType.Int, 4), new SqlParameter("@Note", SqlDbType.NVarChar, 0xff), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            parameters[0].Value = model.RoleID;
            parameters[1].Value = model.RoleName;
            parameters[2].Value = model.RoleFlag;
            parameters[3].Value = model.AdminNum;
            parameters[4].Value = model.Note;
            parameters[5].Value = model.OrderID;
            return DbHelperSQL.RunProcedure("AdminRole_ADD", parameters, out num);
        }

        public int Delete(int RoleID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            parameters[0].Value = RoleID;
            return DbHelperSQL.RunProcedure("AdminRole_Delete", parameters, out num);
        }

        public int GetMaxOrderID()
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[0];
            return DbHelperSQL.RunProcedure("AdminRole_GetMaxOrderID", parameters, out num);
        }

        public KeRui.Model.AdminRole GetModel(int RoleID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4) };
            parameters[0].Value = RoleID;
            KeRui.Model.AdminRole role = new KeRui.Model.AdminRole();
            DataSet set = DbHelperSQL.RunProcedure("AdminRole_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["RoleID"].ToString() != "")
                {
                    role.RoleID = int.Parse(set.Tables[0].Rows[0]["RoleID"].ToString());
                }
                role.RoleName = set.Tables[0].Rows[0]["RoleName"].ToString();
                role.RoleFlag = set.Tables[0].Rows[0]["RoleFlag"].ToString();
                if (set.Tables[0].Rows[0]["AdminNum"].ToString() != "")
                {
                    role.AdminNum = int.Parse(set.Tables[0].Rows[0]["AdminNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    role.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
                }
                role.Note = set.Tables[0].Rows[0]["Note"].ToString();
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    role.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    role.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                return role;
            }
            return null;
        }

        public int Update(KeRui.Model.AdminRole model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4), new SqlParameter("@RoleName", SqlDbType.NVarChar, 20), new SqlParameter("@RoleFlag", SqlDbType.VarChar), new SqlParameter("@Note", SqlDbType.NVarChar, 0xff), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            parameters[0].Value = model.RoleID;
            parameters[1].Value = model.RoleName;
            parameters[2].Value = model.RoleFlag;
            parameters[3].Value = model.Note;
            parameters[4].Value = model.OrderID;
            return DbHelperSQL.RunProcedure("AdminRole_Update", parameters, out num);
        }

        public int UpdateRoleFlagForSiteFlag(KeRui.Model.AdminRole model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@RoleID", SqlDbType.Int, 4), new SqlParameter("@RoleFlag", SqlDbType.VarChar) };
            parameters[0].Value = model.RoleID;
            parameters[1].Value = model.RoleFlag;
            return DbHelperSQL.RunProcedure("AdminRole_UpdateRoleFlagForSite", parameters, out num);
        }
    }
}

